package db

import (
	"fmt"
	"io"
	"log"
	"strings"
	"sync"
)

var (
	logWriter io.Writer
	logMutex  sync.Mutex
)

func SetLogOutput(writer io.Writer) {
	logMutex.Lock()
	defer logMutex.Unlock()
	logWriter = writer
}

func logInfo(message string) {
	write(message, "info")
}

func logError(message string) {
	write(message, "error")
}

func logDebug(message string) {
	write(message, "debug")
}

func write(message string, errorLevel string) {
	fmtMessage := "[db " + strings.ToUpper(errorLevel) + "] " + message
	if logWriter == nil {
		log.Println(fmtMessage)
		return
	}
	logMutex.Lock()
	defer logMutex.Unlock()
	fmt.Fprintln(logWriter, fmtMessage)
}
